package hu.fic.weddingstorefinder.listadapter;

import hu.fic.weddingstorefinder.R;
import hu.fic.weddingstorefinder.parcel.StoreParcelable;
import hu.fic.weddingstorefinder.utils.Utils;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class StoreListAdapter extends BaseAdapter {
	
	private ArrayList<StoreParcelable> storeList;
	
	public StoreListAdapter(ArrayList<StoreParcelable> stores) {
		this.storeList = stores;
	}

	@Override
	public int getCount() {
		return storeList.size();
	}

	@Override
	public Object getItem(int position) {
		return storeList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final StoreParcelable store = storeList.get(position);

		LayoutInflater inflater = (LayoutInflater) parent.getContext()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View itemView = inflater.inflate(R.layout.store_row, null);
		
		TextView storeNameTextView = (TextView)itemView.findViewById(R.id.storerow_name);
		storeNameTextView.setText(store.getPoiName());
		
		TextView storeAddressTextView = (TextView)itemView.findViewById(R.id.storerow_address);
		storeAddressTextView.setText(store.getAddress());
		
		TextView storeDistanceTextView = (TextView)itemView.findViewById(R.id.storerow_distance);
		String distanceText = "";
		if (store.getDistanceFromCurrentLocation() > 0) {
			distanceText = Utils.formatDistance(store.getDistanceFromCurrentLocation());
		}
		storeDistanceTextView.setText(distanceText);
		
		return itemView;
	}

	

}
